package custom;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputListener;
import javax.swing.JSlider;


public class KevinSlider extends JComponent implements MouseInputListener
{
	public static final int FULL_WIDTH=150;
	private KevinSliderUser user;
	
	private int location;
	
	private BufferedImage beginLine;
	private BufferedImage centerLine;
	private BufferedImage endLine;
	private BufferedImage slideyButton;
	
	public KevinSlider(KevinSliderUser use)
	{
		this(use, 0);
	}
	
	public KevinSlider(KevinSliderUser use, int loc)
	{
		initializeImages();
		user=use;
		//location=loc;
		location=percentToLoc(loc);
		
		addMouseListener(this);
		addMouseMotionListener(this);
		setPreferredSize(new Dimension(FULL_WIDTH,slideyButton.getHeight()));
		
		
		
		/*if (location<slideyButton.getWidth()/2)
		{
			location=slideyButton.getWidth()/2;
		}*/
	}
	
	
	
	public KevinSlider getThis()
	{
		return this;
	}
	
	public void initializeImages()
	{
		beginLine = null;
		centerLine = null;
		endLine = null;
		
		slideyButton = null;
		try {
			beginLine = ImageIO.read(KevinSlider.class.getResource("/resources/images/custom/kevinslider/line/redlinebegin1.png"));
		    centerLine = ImageIO.read(KevinSlider.class.getResource("/resources/images/custom/kevinslider/line/redlinecenter1.png"));
		    endLine = ImageIO.read(KevinSlider.class.getResource("/resources/images/custom/kevinslider/line/redlineend1.png"));
		    slideyButton = ImageIO.read(KevinSlider.class.getResource("/resources/images/custom/kevinslider/red1.png"));
		} catch (IOException e) {
		}
	}
	
	public void paint(Graphics g)
	{
		Dimension d=this.getSize();
		int y=d.height/2-centerLine.getHeight()/2;
		
		g.drawImage(beginLine, 0, y, null, null);
		for (int x = beginLine.getWidth(); x < d.width-endLine.getWidth(); x += centerLine.getWidth())
		{
			g.drawImage(centerLine, x, y, null, null);
		}
		g.drawImage(endLine, d.width-endLine.getWidth(), y, null,null);
		
		g.drawImage(slideyButton, location-slideyButton.getWidth()/2, 0, null,null);//(int)(((double)location/100)*d.width), 0, null, null);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		location=arg0.getX();
		
		if (location<slideyButton.getWidth()/2)
		{
			location=slideyButton.getWidth()/2;
		}
		if(location>getLineWidth())
		{
			location=getLineWidth();
		}
		repaint();
		user.kevinSliderActivated(this);
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public int getLineWidth()
	{
		int lineWidth=(int) (this.getWidth()-(slideyButton.getWidth()/2));
		if (lineWidth<0)
		{
			lineWidth=(int) (FULL_WIDTH-(slideyButton.getWidth()/2));
		}
		//System.out.println(this.getPreferredSize().getWidth());
		return lineWidth;
	}
	
	public int getValue()
	{
		return locToPercent(location);
	}
	
	public void setValue(int value)
	{
		location=percentToLoc(value);
	}
	
	public int locToPercent(int loc)
	{
		return ((loc-slideyButton.getWidth()/2)*100/(getLineWidth()-slideyButton.getWidth()/2));
	}
	
	public int percentToLoc(int percent)
	{
		
		//int toReturn=((percent*(getLineWidth()-slideyButton.getWidth()/2))/100)+slideyButton.getWidth()/2;
		int loc=((percent*(getLineWidth()-slideyButton.getWidth()/2))/100)+slideyButton.getWidth()/2;
		return loc;
	}
	
}
